package com.PVZ.engine;

import com.PVZ.engine.map.GameMap;
import com.PVZ.engine.map.MapBlock;
import com.PVZ.model.Entity;
import com.PVZ.model.bullet.Bullet;
import com.PVZ.model.interfaces.Gettable;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.zombie.Zombie;
import com.PVZ.view.GameFrame;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * Created by @Author: kabuda on @Date: 2021/5/30 9:59
 * <p>
 * 清理类 清理已死亡的类 health<=0
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Cleaner {
    private Stage stage;

    /**
     * 删除单个单元格内的一个植物
     *
     * @param mapBlock 单元格
     */
    public void clearSinglePlant(MapBlock mapBlock) {
        List<Plant> allPlantList = stage.getPlantFactory().getAllPlantsList();
        Plant plant = mapBlock.getPlants().get(0);
        if (plant != null) {
            plant.setIsAlive(Entity.DEAD);

            mapBlock.getPlants().remove(0);
            plant.setPoint(null);
            allPlantList.remove(plant);
        }

        if(mapBlock.getPlants().size()==0){
            mapBlock.setHasPlant(false);
        }

    }

    /**
     * 删除某个植物
     *
     * @param plant 单个植物
     */
    public void clearSinglePlant(Plant plant) {
        List<Plant> allPlantList = stage.getPlantFactory().getAllPlantsList();
        GameMap.getBlockByCoor(plant.getRow(), plant.getCol()).removeEntity(plant);
        plant.setPoint(null);
        allPlantList.remove(plant);
    }

    /**
     * 清理不需要的实体类
     * 超过边界 血量<=0
     */
    public void updateEntityFromStage(Entity entity, Stage stage) {
        boolean flag = false;

        if(entity==null){
            return;
        }
        if (entity.getPoint()==null){
            flag = true;
        }
        if (entity.getPoint() != null) {

            if(entity.getPoint().x < 20 ||entity.getPoint().y < 0){
                if(entity instanceof Zombie){
                    stage.setIsOver(true);
                }else{
                    flag = true;
                }
            }
            /*
            死亡或者超出边界
             */
            if (entity.getIsAlive() == Entity.DEAD ||  entity.getPoint().x >= GameFrame.WIDTH ||  entity.getPoint().y > GameFrame.HEIGHT) {
                flag = true;
            } else {
                flag = false;
            }
        }

        if(flag){
            //从list中移除
            if (entity instanceof Gettable) {
                //若添加元素是可拾取类
                if (stage.getGettableFactory().getAllGettableList().contains(entity)) {
                    boolean b = stage.getGettableFactory().getAllGettableList().remove((Gettable) entity);
                    b = true;
                }
            } else if (entity instanceof Zombie) {
                if (stage.getZombieFactory().getAllZombieList().contains(entity)) {
                    stage.getZombieFactory().getAllZombieList().remove((Zombie) entity);
                }
            } else if (entity instanceof Plant) {
                if (stage.getPlantFactory().getAllPlantsList().contains(entity)) {
                    stage.getPlantFactory().getAllPlantsList().remove((Plant) entity);
                }
            } else if (entity instanceof Bullet) {
                if (stage.getBulletFactory().getAllBulletList().contains(entity)) {
                    stage.getBulletFactory().getAllBulletList().remove((Bullet) entity);
                }
            }

            stage.getAllEntityList().remove(entity);
        }
    }

    public static void clearEntityFromBlock(Entity entity,MapBlock block) {
        if (entity instanceof Gettable) {
            //若添加元素是可拾取类
            block.getGettableList().remove(entity);
        } else if (entity instanceof Zombie) {
            block.getZombies().remove(entity);
        } else if (entity instanceof Plant) {
           block.getPlants().remove(entity);
        } else if (entity instanceof Bullet) {
            block.getBullets().remove(entity);
        }
    }

}
